/**
 * Created by Administrator on 2017/12/27.
 * 菜单管理JS
 */
layui.config({
    base: '../build/js/'
}).use(['ztree', 'layer','table','form'], function() {
    var $ = layui.jquery,
        layer = layui.layer
        ,table = layui.table
        ,form = layui.form
        ,selectNode=null
        ,addDepartmentLayer=null;

    /**
     * 加载菜单树
     * @type {Array}
     */
    var zNodes = [];
    var loadParam={pId:1,keyWord:""};
    var setting = {
        view: {
            selectedMulti: false
        },
        check: {
            enable: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        edit: {
            enable: false
        },
        callback: {
            onClick: zTreeOnClick
        }
    };


    $(document).ready(function() {
        //初始化树
        initZTree();
    });

    //初始化树
    function initZTree() {
        $.post("/department/select", {pId:0,keyWord:""}, function(res) {
            if(res){
                if(res.code==0){
                    zNodes=res.data;
                    var zTreeObj =$.fn.zTree.init($("#ztree"), setting, zNodes);
                    zTreeObj.expandAll(true);
                }else {
                    layer.msg(res.msg, {
                        icon: 2,
                        time:3000
                    });
                }
            }
        }, 'json');
    }

    function zTreeOnClick(event, treeId, treeNode) {
        selectNode=treeNode;
        loadParam.pId=treeNode.id;
        loadParam.keyWord="";
        table.reload('deptTable', {
            where: loadParam
        });
    };

    //加载子级菜单列表
    table.render({
        elem: '#departmentTable'
        ,url:'../department/select'
        ,where:loadParam
        ,method:'post'
        ,cellMinWidth: 80
        ,cols: [[
            {type: 'checkbox'}
            ,{field:'name', title:'名称',edit: 'text'}
            ,{field:'desc', title:'描述',minWidth:120,edit: 'text'}
            ,{field:'order', title: '排序',minWidth:40,edit: 'text'}
            ,{fixed: 'right', width:100, align:'center', toolbar: '#barMenus'}
        ]]
        ,id: 'deptTable'
    });

    //监听单元格编辑
    table.on('edit(departmentTable)', function(obj){
        var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field; //得到字段
        data[field]=value;
        $.post("/department/updateDepartment",data, function(res) {
            if(res){
                if(res.code==0){
                    initZTree();
                    table.reload('deptTable', {
                        where: loadParam
                    });
                }else{
                    layer.msg(res.msg, {
                        icon: 2
                    });
                }
            }
        }, 'json');
    });

    //监听工具条
    table.on('tool(departmentTable)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            layer.confirm('真的删除部门信息么', function(index){
                var departmentIds=[];
                departmentIds.push(data.id);
                excDeleteDepartmentIds(departmentIds);
            });
        }
    });

    function excDeleteDepartmentIds(departmentIds) {
        $.post("../department/deleteDepartments",{departmentIds:departmentIds}, function(res) {
            if(res){
                if(res.code==0){
                    layer.msg(res.msg, {
                        icon: 1,
                        time: 3000 //3s后自动关闭
                    });
                    table.reload('deptTable', {
                        where: loadParam
                    });
                }else{
                    layer.msg(res.msg, {
                        icon: 2,
                        time:3000
                    });
                }
            }
        }, 'json');
    }


    var active = {
        addDepartment:function () {
            if(selectNode){
                addDepartmentLayer=layer.open({
                    type:1,
                    area:'30%',
                    offset:'20%',
                    maxmin:true,
                    skin:'layui-layer-molv',
                    content:$("#addDepartmentForm")
                });
            }else{
                layer.msg("请选择父级部门", {
                    icon: 2,
                    time:3000
                });
            }
        }
    };

    $('.departmentToolBar .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //添加
    form.on('submit(addDepartmentSubmit)', function(data) {
        var loadIndex = layer.load(2, {
            shade: [0.3, '#333']
        });
        data.field.pId=selectNode.id;
        $.post("../department/addDepartment", data.field, function(res) {
            loadIndex && layer.close(loadIndex);
            if(res.code==0){
                layer.msg(res.msg, {
                    icon: 1,
                    time:3000
                });
                layer.close(addDepartmentLayer);
                table.reload('deptTable', {
                    where: loadParam
                });
            }else{
                layer.msg(res.msg, {
                    icon: 2,
                    time:3000
                });
            }
        }, 'json');
    });

});
